package com.example.composestudy.router.part1.home

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavGraph
import com.example.composestudy.router.RouterConfig
import com.example.composestudy.router.TAG

@Composable
fun HomePager(navController: NavController, name: String, age: Int) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("首页") },
                navigationIcon = {
                    Button(onClick = {
                        navController.navigateUp()
                    }) {
                        Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "返回")
                    }
                }
            )
        },
        content = { innerPadding ->
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp)
                    .padding(innerPadding)
            ) {
                Text(text = "欢迎 $name, 你的年龄是 $age")
                Button(onClick = {
                    navController.navigate(RouterConfig.WELCOME) {
                        popUpTo(RouterConfig.WELCOME) {
                            inclusive = true
                        }
                    }
                }) {
                    Text("返回欢迎页面")
                }
                Button(onClick = {
                    navController.navigate(RouterConfig.PART_TWO)
                }) {
                    Text("进入第二部分")
                }
            }
        }
    )
    Log.d(TAG, "HomePager: 堆栈信息：\n" +
            "backQueue(当前后退堆栈)：${navController.backQueue.size}\n" +
            "destinationCountOnBackStack(后退堆栈上的非导航图目标数): ${
                navController.backQueue.count { entry ->
                    entry.destination !is NavGraph
                }
            }")
}